package com.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.objenesis.strategy.StdInstantiatorStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.service.StudentManagerService;
import com.spring.model.Student;

@Controller
@RequestMapping(value="/controller")
public class StudentController {
	@Resource(name="studentManager")
	private StudentManagerService studentManagerService;

	public void setStudentManagerService(StudentManagerService studentManagerService) {
		this.studentManagerService = studentManagerService;
	}
	@RequestMapping(value="/addStudent",method=RequestMethod.POST)
    
	public  String addStudent(Student student,HttpServletResponse response) throws IOException
	{
		studentManagerService.addStudent(student);
		System.out.println("nihao"+ student.toString());
		response.sendRedirect("/spring/controller/getAllStudent");
		return null;
	}
	@RequestMapping(value="/toaddStudent")
	public String toaddStudent()
	{
		return "index";
	}
	@RequestMapping(value="/getAllStudent",method=RequestMethod.GET)
public String getAllStudent(HttpServletRequest request)
{
	request.setAttribute("studentList", studentManagerService.getAllStudent());
	return "query";
	
}
	@RequestMapping(value="/delStudent")
	public void delStudent(String id,HttpServletResponse response) throws IOException
	{
		studentManagerService.delStudent(id);
		
		
		response.sendRedirect("/spring/controller/getAllStudent");
	
		
	}
	@RequestMapping("/toupdateStudent")
	public String getStudent(String id,HttpServletRequest request)
	{
		Student student=studentManagerService.getStudent(id);
		request.setAttribute("student", student);
		return "update";
	}
	@RequestMapping(value="/updateStudent")
	public void updateStudent(Student student,HttpServletResponse response,HttpServletRequest request) throws IOException
	{
		student.setName(request.getParameter("name"));
		student.setAge(request.getParameter("age"));
		System.out.print(student.getName());
		studentManagerService.updateStudent(student);
	   student=studentManagerService.getStudent(student.getId());
	   request.setAttribute("student", student);
	          response.sendRedirect("/spring/controller/getAllStudent");
	}
	@RequestMapping(value="/login")
	public String login(String user,String password)
	{
		boolean flag=studentManagerService.login(user, password);
		if(flag)
		{
			return "Sucess";
		}else
		{
			return "index";
		}
		
		
		
		
	}
}

